package MiniFrame;

import Shapes.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.util.*;

import javax.swing.*;

public class MiniFrame extends JFrame {
	
	private static final int DEFAULT_WIDTH = 800;
	private static final int DEFAULT_HEIGHT = 500;
	
	private ArrayList<Shape> shapes;
	private Shape current;

	private int shapeNum;
	private boolean isExisted;

	public MiniFrame()
	{	
		
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		MiniComponent mini1 = new MiniComponent();
		add(mini1);
		add(new ControlPanel(mini1), BorderLayout.EAST);

	}
	
	
	public class ControlPanel extends JPanel {
		private JPanel buttonPanel;
		private JPanel colorButtonPanel;
		private MiniComponent mini1;

		public ControlPanel(MiniComponent mini1) {
			// TODO Auto-generated constructor stub
			this.mini1 = mini1;
			//Create buttons
			JButton lineButton = new JButton("Line");
			JButton circleButton = new JButton("Circle");
			JButton rectangleButton = new JButton("Rectangle");
			JButton textButton = new JButton("Text");

			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridLayout(4, 1));

			//Add buttons to panel
			buttonPanel.add(rectangleButton);
			buttonPanel.add(lineButton);
			buttonPanel.add(circleButton);
			buttonPanel.add(textButton);

			//Create color buttons
			JButton blackButton = new JButton();
			blackButton.setBackground(Color.BLACK);
			blackButton.setOpaque(true);
			blackButton.setBorderPainted(false);
			JButton darkgrayButton = new JButton();
			darkgrayButton.setBackground(Color.DARK_GRAY);
			darkgrayButton.setOpaque(true);
			darkgrayButton.setBorderPainted(false);
			JButton lightgrayButton = new JButton();
			lightgrayButton.setBackground(Color.LIGHT_GRAY);
			lightgrayButton.setOpaque(true);
			lightgrayButton.setBorderPainted(false);
			JButton blueButton = new JButton();
			blueButton.setBackground(Color.BLUE);
			blueButton.setOpaque(true);
			blueButton.setBorderPainted(false);
			JButton cyanButton = new JButton();
			cyanButton.setBackground(Color.CYAN);
			cyanButton.setOpaque(true);
			cyanButton.setBorderPainted(false);
			JButton greenButton = new JButton();
			greenButton.setBackground(Color.GREEN);
			greenButton.setOpaque(true);
			greenButton.setBorderPainted(false);
			JButton redButton = new JButton();
			redButton.setBackground(Color.RED);
			redButton.setOpaque(true);
			redButton.setBorderPainted(false);
			JButton magentaButton = new JButton();
			magentaButton.setBackground(Color.MAGENTA);
			magentaButton.setOpaque(true);
			magentaButton.setBorderPainted(false);
			JButton pinkButton = new JButton();
			pinkButton.setBackground(Color.PINK);
			pinkButton.setOpaque(true);
			pinkButton.setBorderPainted(false);
			JButton orangeButton = new JButton();
			orangeButton.setBackground(Color.ORANGE);
			orangeButton.setOpaque(true);
			orangeButton.setBorderPainted(false);
			JButton yellowButton = new JButton();
			yellowButton.setBackground(Color.YELLOW);
			yellowButton.setOpaque(true);
			yellowButton.setBorderPainted(false);
			JButton grayButton = new JButton();
			grayButton.setBackground(Color.GRAY);
			grayButton.setOpaque(true);
			grayButton.setBorderPainted(false);

			//Add colors to panel
			colorButtonPanel = new JPanel();
			colorButtonPanel.setLayout(new GridLayout(4, 3));
			colorButtonPanel.add(blackButton);
			colorButtonPanel.add(darkgrayButton);
			colorButtonPanel.add(lightgrayButton);
			colorButtonPanel.add(blueButton);
			colorButtonPanel.add(cyanButton);
			colorButtonPanel.add(greenButton);
			colorButtonPanel.add(redButton);
			colorButtonPanel.add(magentaButton);
			colorButtonPanel.add(pinkButton);
			colorButtonPanel.add(orangeButton);
			colorButtonPanel.add(yellowButton);
			colorButtonPanel.add(grayButton);

			this.setLayout(new GridLayout(2, 1));
			this.add(buttonPanel);
			this.add(colorButtonPanel);

			//Add draw actions
			DrawAction drawCircleAction = new DrawAction("circle");
			DrawAction drawLineAction = new DrawAction("line");
			DrawAction drawRectangleAction = new DrawAction("rectangle");
			DrawAction drawTextAction = new DrawAction("text");
			lineButton.addActionListener(drawLineAction);
			circleButton.addActionListener(drawCircleAction);
			rectangleButton.addActionListener(drawRectangleAction);
			textButton.addActionListener(drawTextAction);

			//Add color actions
			ColorAction blackAction = new ColorAction(Color.BLACK);
			ColorAction darkgrayAction = new ColorAction(Color.DARK_GRAY);
			ColorAction lightgrayAction = new ColorAction(Color.LIGHT_GRAY);
			ColorAction blueAction = new ColorAction(Color.BLUE);
			ColorAction cyanAction = new ColorAction(Color.CYAN);
			ColorAction greenAction = new ColorAction(Color.GREEN);
			ColorAction redAction = new ColorAction(Color.RED);
			ColorAction magentaAction = new ColorAction(Color.MAGENTA);
			ColorAction pinkAction = new ColorAction(Color.pink);
			ColorAction orangeAction = new ColorAction(Color.ORANGE);
			ColorAction yellowAction = new ColorAction(Color.YELLOW);
			ColorAction grayAction = new ColorAction(Color.GRAY);
			blackButton.addActionListener(blackAction);
			darkgrayButton.addActionListener(darkgrayAction);
			lightgrayButton.addActionListener(lightgrayAction);
			blueButton.addActionListener(blueAction);
			cyanButton.addActionListener(cyanAction);
			greenButton.addActionListener(greenAction);
			redButton.addActionListener(redAction);
			magentaButton.addActionListener(magentaAction);
			pinkButton.addActionListener(pinkAction);
			orangeButton.addActionListener(orangeAction);
			yellowButton.addActionListener(yellowAction);
			grayButton.addActionListener(grayAction);

		}


		private class DrawAction implements ActionListener
		{
			String shapeName;

			public DrawAction(String shapeName) {
				this.shapeName = shapeName;
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("Press!");
				switch(shapeName)
				{
					case "circle": shapeNum = 1;break;
					case "line": shapeNum = 2;break;
					case "rectangle": shapeNum = 3;break;
					case "text": shapeNum = 4;break;
				}
			}

		}

		private class ColorAction implements ActionListener
		{
			private Color c;

			public ColorAction(Color c) {
				this.c = c;
			}


			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				current.setColor(c);
				mini1.repaint();
			}
		}

	}
	
	public class MiniComponent extends JComponent
	{

		public MiniComponent()
		{
			shapes = new ArrayList<>();
			current = null;
			shapeNum = 0;

			addMouseListener(new MouseHandler());
			addMouseMotionListener(new MouseMotionHandler());
		}
		@Override
		protected void paintComponent(Graphics g) {
			// TODO Auto-generated method stub
			Graphics2D g2 = (Graphics2D) g;
			if(!shapes.isEmpty())
			for(Shape s : shapes)
				s.draw(g2);
		}
		
		public Shape find(Point2D p)
		{
			for (Shape s : shapes) {
				if(s.findShape(p)) return s;
			}
			return null;
		}
		
		public void add()
		{
			if(shapeNum == 0) return;
			switch (shapeNum)
			{
				case 1: current = new Circle(); break;
				case 2: current = new Line(); break;
				case 3: current = new Rectangle(); break;
				case 4: current = new Text(); break;
			}
			current.shapeInit(getMousePosition());
			shapes.add(current);
			repaint();
		}
		
		class MouseHandler extends MouseAdapter
		{
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				current = find(e.getPoint());
				isExisted = false;
				if(current == null) add();
				else isExisted = true;
				repaint();
			}
		}

		class MouseMotionHandler implements MouseMotionListener
		{

			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				if(!isExisted){
					current.setShape(current.getStart(), e.getPoint());
				}
				else{
					current.moveShape(getMousePosition());
				}
				repaint();
			}

			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub

				if(find(e.getPoint())==null) setCursor(Cursor.getDefaultCursor());
				else setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
			}

		}
	}
}
